let getMoneyAmount = function(n){
    let arr = new Array(n + 1)
    for(let i=0; i<=n; i++){
        arr[i] = new Array(n + 1)
    }
    for(let i=0; i<=n; i++){
        for(let j=0; j<=n;j++){
            if(i+1=== j){
                arr[i][j] = i
            }else{
                arr[i][j] = 0
            }
            
        }
    }
    console.log(arr)
    for(let i=1; i<=n; i++){
        for(let j=1; j<=n; j++){
            if(i===j){
                arr[i][j] = 0
            }else{
                arr[i][j] = i + arr[i+1][j]
                for(let x=i+1; x<=j;x++){
                    arr[i][j] = Math.min(arr[i][j], x + Math.max(arr[1][x-1], arr[x+1][j]))
                }
            }
            console.log(i, j, arr[i][j])
        }
    }
    console.log(arr[1][n])
}

getMoneyAmount(4)